package cn.com.dhcc.ddi.task.service.transform.impl;

import cn.com.dhcc.app.core.util.JsonUtil;
import cn.com.dhcc.ddi.task.service.transform.FuncHandlerInterface;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 反射调用函数
 * @日期：2016/11/24 15:39
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Service
public class DefFuncHandler implements FuncHandlerInterface {
    private static String className = "cn.com.dhcc.ddi.task.service.transform.FunctionAction";

    /**
     * 调用函数
     * @param methodName 函数名
     * @param args参数list
     * @return
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws InvocationTargetException
     */
    public Object call(String methodName, List args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
            Class<?> clz = Class.forName(className);
            Object o = clz.newInstance();
            Map<String,Method> methodMap = getMethods(clz);
            Method method = methodMap.get(methodName);
            Class<?>[] parameterTypes = method.getParameterTypes();
            Object[] obj = new Object[parameterTypes.length];
            for (int i = 0; i < parameterTypes.length; i++) {
                Class<?> clas = parameterTypes[i];
                obj[i] = ConvertUtils.convert(args.get(i), clas);

            }
            return method.invoke(o, obj);
    }
    /**
     * 获取定义方法
     * @param clazz
     * @return
     */
    public static Map<String,Method> getMethods(Class clazz){
        Map<String,Method> methodMap = new HashMap<String,Method>();
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            String methodName = method.getName();
            methodMap.put(methodName, method);
        }
        return methodMap;
    }

    private static void getMethodInfo(String className) throws ClassNotFoundException {
            Class clazz = Class.forName(className);
            Map<String,Method> methodMap = new HashMap<String,Method>();
            Method[] methods = clazz.getMethods();
            for (Method method : methods) {
                String methodName = method.getName();
                methodMap.put(methodName, method);
                System.out.println("方法名称:" + methodName);
                Class<?>[] parameterTypes = method.getParameterTypes();
                for (Class<?> clas : parameterTypes) {
                    String parameterName = clas.getName();
                    System.out.println("参数名称:" + parameterName);
                }
            }
    }

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<String, String>();
        map.put("arg1","111");
        map.put("arg2","222");
        List<Map> list = new ArrayList<Map>();
        list.add(map);
        System.out.println(JsonUtil.toJson(list));
    }

}
